cmake_minimum_required(VERSION 3.8.0 FATAL_ERROR)

project(libmod1-shared-ab)

option(BUILD_SHARED_MOD1_SHARED_AB "Build libmod1-shared-ab as a shared library." ON)
option(BUILD_STATIC_MOD1_SHARED_AB "Build libmod1-shared-ab as a static library." OFF)

include(${CMAKE_CURRENT_SOURCE_DIR}/../../../mkspecs/cmake/global.cmake)
include(${CMAKE_CURRENT_SOURCE_DIR}/../../../mkspecs/cmake/version.cmake)

file(GLOB_RECURSE API_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/api/*.h)
file(GLOB_RECURSE SRC_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/src/*.h)
file(GLOB_RECURSE SRC_CPPS    ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
set(SOURCES ${API_HEADERS} ${SRC_HEADERS} ${SRC_CPPS})
if(WIN32)
    list(APPEND SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/mod1-shared-ab.rc)
endif()

if(WIN32)
    add_definitions(-DMOD1_EXPORTS)
endif()

include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}/api
    ${CMAKE_CURRENT_SOURCE_DIR}/src
    ${CYG_NOPUB_INC_PATH}/mod1-static-a
    ${CYG_NOPUB_INC_PATH}/mod1-static-b
    )

link_directories(
    ${CYG_NOPUB_LIB_PATH})

link_libraries(
    debug mod1-static-a${CMAKE_DEBUG_POSTFIX} optimized mod1-static-a
    debug mod1-static-b${CMAKE_DEBUG_POSTFIX} optimized mod1-static-b
    )

list(APPEND API_HEADERS
    ${CYG_NOPUB_INC_PATH}/mod1-static-a/imod1-static-a.h
    ${CYG_NOPUB_INC_PATH}/mod1-static-a/fmod1-static-a.h
    ${CYG_NOPUB_INC_PATH}/mod1-static-b/imod1-static-b.h
    ${CYG_NOPUB_INC_PATH}/mod1-static-b/fmod1-static-b.h)

if(BUILD_SHARED_MOD1_SHARED_AB)
    add_library(mod1-shared-ab SHARED ${SOURCES})
    target_link_libraries(mod1-shared-ab)

    add_custom_command(TARGET mod1-shared-ab POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E make_directory                            ${CYG_INC_PATH}/mod1-shared-ab
        COMMAND ${CMAKE_COMMAND} -E copy ${API_HEADERS}                       ${CYG_INC_PATH}/mod1-shared-ab
        COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:mod1-shared-ab>        ${CYG_LIB_PATH}/$<TARGET_FILE_NAME:mod1-shared-ab>
        COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_LINKER_FILE:mod1-shared-ab> ${CYG_LIB_PATH}/$<TARGET_LINKER_FILE_NAME:mod1-shared-ab>
        COMMENT "This command will be executed after building target mod1-shared-ab")

    install(FILES $<TARGET_FILE:mod1-shared-ab>        DESTINATION ${INSTALL_LIB_PATH})
    install(FILES $<TARGET_LINKER_FILE:mod1-shared-ab> DESTINATION ${INSTALL_LIB_PATH})
    if(MSVC)
        install(FILES $<TARGET_PDB_FILE:mod1-shared-ab> DESTINATION ${INSTALL_LIB_PATH} CONFIGURATIONS Debug)
    endif()
endif()

if(BUILD_STATIC_MOD1_SHARED_AB)
    add_library(mod1-shared-ab_static STATIC ${SOURCES})
    set_target_properties(mod1-shared-ab_static PROPERTIES OUTPUT_NAME "mod1-shared-ab")
endif()

# install(FILES ${API_HEADERS} DESTINATION include/mod1-shared-ab)
